package org.jpro.config;

import java.util.Properties;

import org.jpro.datasource.DynamicDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/**
 * <P>功能描述 : quartz定时任务配置 </P>
 * <P>File name : QuartzConfig </P>
 * <P>Author : 王泽浩 </P>
 * <P>E-Mail : 1028625100@qq.com </P>
 * <P>Date : 2017/3/8 下午4:11</P>
 */
@Slf4j
@Setter
@Getter
@ToString
@Configuration
@ConfigurationProperties(prefix = "spring.quartz")
public class QuartzConfig {

    /**
     * 名称
     */
    private String schedulerName = "ClusterScheduler";

    /**
     * 设置自动启动
     */
    private boolean autoStartup = true;

    /**
     * 可选，QuartzScheduler 启动时更新己存在的Job，这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
     */
    private boolean overwriteExistingJobs = true;

    /**
     * 
     */
    private String applicationContextSchedulerContextKey = "applicationContextKey";

    /**
     * 必须的，QuartzScheduler 延时启动，应用启动完后 QuartzScheduler 再启动
     */
    private int startupDelay = 30;

    /**
     * quartz基本配置
     */
    private Properties properties = new Properties();

    public QuartzConfig() {
        properties.put("org.quartz.scheduler.instanceName", "ClusterScheduler");
        properties.put("org.quartz.scheduler.instanceId", "AUTO");

        // 线程池配置
        properties.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
        properties.put("org.quartz.threadPool.threadCount", "20");
        properties.put("org.quartz.threadPool.threadPriority", "5");

        // JobStore 配置
        properties.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");

        // 集群配置
        properties.put("org.quartz.jobStore.isClustered", "true");
        properties.put("org.quartz.jobStore.clusterCheckinInterval", "15000");
        properties.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
        properties.put("org.quartz.jobStore.misfireThreshold", "12000");
        properties.put("org.quartz.jobStore.tablePrefix", "QRTZ_  ");
    }

    @Bean
    @DependsOn("dataSource")
    public SchedulerFactoryBean schedulerFactory(@Qualifier("dataSource") DynamicDataSource dataSource) {

        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setDataSource(dataSource.getWrite().dataSource());// 这里先强制制定为读写数据源，之后可以改成读写分离

        schedulerFactoryBean.setQuartzProperties(properties);// 基本配置
        schedulerFactoryBean.setSchedulerName(schedulerName);

        // 设置自动启动
        schedulerFactoryBean.setAutoStartup(autoStartup);
        return schedulerFactoryBean;

    }

}
